package com.homework2.demo2;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class test {
    public static void main(String[] args) {
        Student student1 = new Student("张三",new HashSet<Course>());
        student1.addCourse(new Course("语文"));
        student1.addCourse(new Course("数学"));
        student1.addCourse(new Course("英语"));

        Student student2 = new Student("李四",new HashSet<Course>());
        student2.addCourse(new Course("语文"));
        student2.addCourse(new Course("数学"));

        Student student3 = new Student("王五",new HashSet<Course>());
        student3.addCourse(new Course("数学"));
        student3.addCourse(new Course("英语"));


        SchoolClass class1 = new SchoolClass("一年级一班",new HashSet<Student>());
        class1.addStudent(student1);
        class1.addStudent(student2);
        class1.addStudent(student3);

        //统计出每门课程的选课人数
        HashMap<String,Integer> resultMap = new HashMap();
        HashSet<Student> students = class1.getStudents();
        for (Student student:students){
            HashSet<Course> courses = student.getCourses();
            for (Course course:courses){
                Integer value = resultMap.get(course.getName());
                resultMap.put(course.getName(), value==null?1:value+1);
            }
        }
        Set<Map.Entry<String, Integer>> entries = resultMap.entrySet();
        for (Map.Entry entry:entries){
            System.out.println(entry.getKey()+":"+entry.getValue());
        }

    }
}
